package com.devgroupvn.dota.items;

import java.util.List;

import com.devgroupvn.dota.Application;
import com.devgroupvn.dota.R;
import com.kin.bitmapcatching.ImageDownloader;
import com.kin.bitmapcatching.DisplayBitmap.ObserveCatching;
import com.kin.bitmapcatching.ImageDownloader.AsyncDownloadDrawable;
import com.kin.bitmapcatching.ImageDownloader.BitmapDownloaderTask;

import view.NewsPage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemListAdapter extends BaseAdapter implements ObserveCatching
{
	List<Item> mListItem;
	Context mContext;
	private static final int VIEW_TYPE_GROUP_START = 0;
	private static final int VIEW_TYPE_GROUP_CONT = 1;
	private static final int VIEW_TYPE_COUNT = 2;

	public ItemListAdapter(Context _Context, List<Item> _list)
	{
		mListItem = _list;
		mContext = _Context;
	}

	@Override
	public int getCount()
	{
		return mListItem.size();
	}
/*
	@Override
	public int getItemViewType(int position)
	{
		if (position == 0) { return VIEW_TYPE_GROUP_START; }
		Item entry = mListItem.get(position);
		if (isNewGroup(entry))
		{
			return VIEW_TYPE_GROUP_START;
		}
		else
		{
			return VIEW_TYPE_GROUP_CONT;
		}

	}*/

	@Override
	public int getViewTypeCount()
	{
		return VIEW_TYPE_COUNT;
	}

	@Override
	public Object getItem(int position)
	{
		return mListItem.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}
	private class ViewHolder
	{
		ImageView image;
		TextView name;
//		TextView description;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		LayoutInflater inflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		Item entry = mListItem.get(position);
		ViewHolder holder = new ViewHolder();
		if(convertView==null)
		{
			convertView = inflater.inflate(R.layout.item_list, null);
			holder.image = (ImageView)convertView.findViewById(R.id.image);
			holder.name = (TextView)convertView.findViewById(R.id.name);
//			holder.description = (TextView)convertView.findViewById(R.id.description);
			convertView.setTag(holder);
			
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.name.setText(entry.name);
		if(entry.image!=null)
		{
			loadBitmapWithMemCache(entry.image,holder.image);
		}
//		holder.description .setText(entry.description);
		return convertView;
	}

	private boolean isNewGroup(Item _entry)
	{
		return true;
	}

	@Override
	public void addBitmapToMemCache(String key, Bitmap bitmap)
	{
		// TODO Auto-generated method stub
		if (getBitmapFromMemCache(key)==null)
    	{
			if(bitmap!=null)
			{
				Application.mMemCache.put(key, bitmap);
			}
    	}
    	
    	if (!Application.mDiskCache.containsKey(key))
    	{
    		if(bitmap!=null)
    		{
    			Application.mDiskCache.put(key, bitmap);	
    		}
    	}
	}

	@Override
	public Bitmap getBitmapFromMemCache(String key)
	{
		// TODO Auto-generated method stub
		return Application.mMemCache.get(key);
	}

	@Override
	public Bitmap getBitmapFromDiskCache(String key)
	{
		// TODO Auto-generated method stub
		return Application.mMemCache.get(key);
	}
	 private void loadBitmapWithMemCache(String fileName,ImageView imageView)
	    {
	    	if (ImageDownloader.cancelPotentialWork(fileName, imageView))
	    	{
		    	final String imageKey = String.valueOf(fileName);
		    	final Bitmap bitmap = getBitmapFromMemCache(imageKey);
		    	if (bitmap==null)
		    	{
		    		ImageDownloader imageDownloader = new ImageDownloader(mContext,this);   		
		    		final BitmapDownloaderTask task = imageDownloader.new BitmapDownloaderTask(imageView,mContext);
		    		Bitmap temp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ava);
		    		final AsyncDownloadDrawable asyncDrawable = new AsyncDownloadDrawable(mContext.getResources(),temp, task);
		    		imageView.setImageDrawable(asyncDrawable);
		    		task.execute(fileName);
		    	}
		    	else
		    	{
		    		imageView.setImageBitmap(bitmap);
		    	}
	    	}
	    }
}
